export const colors = [
  'gray',
  'red',
  'yellow',
  'green',
  'blue',
  'indigo',
  'purple',
  'pink',
]

export const colorOptions = colors.map(color => ({
  name: color,
  value: color,
}))

export const conditionOptions = [
  {
    name: 'all conditions are met',
    value: 'all',
  },
  {
    name: 'any condition is met',
    value: 'any',
  }
]

export const conditionTypes = {
  'all': 'AND',
  'any': 'OR',
}

export const operators = [
  'equal',
  'notEqual',
  'in',
  'notIn',
  'contains',
  'doesNotContain',
  'lessThan',
  'lessThanInclusive',
  'greaterThan',
  'greaterThanInclusive',
]

export const operatorOptions = operators.map(operator => ({
  name: operator,
  value: operator,
}))

export function getConditionType(source) {
  return 'all' in source ? 'all' : 'any'
}

export function getIsConditionGroup(source) {
  return 'all' in source || 'any' in source
}

export function findNestedConditions (rule, id, cb) {
  for (const ruleItem of rule) {
    if (getIsConditionGroup(ruleItem)) {
      const type = getConditionType(ruleItem)
      if (ruleItem.id === id) cb(ruleItem[type])
      else findNestedConditions(ruleItem[type], id, cb)
    }
  }
}
